/************************************************************************************
 *                                                                                  *
 *                                                                                  *
 ************************************************************************************/
package jMiiEditor.gui.swingintrospection;

import jMiiEditor.gui.JMiiEditorMainJFrame;
import jMiiEditor.mii.Mii;
import jMiiEditor.mii.MiiTestHelper;
import jMiiEditor.mii.introspection.MiiGetterIntrospectionItem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import cx.ath.choisnet.lang.introspection.IntrospectionException;
import cx.ath.choisnet.lang.introspection.IntrospectionInvokeException;
import cx.ath.choisnet.swing.introspection.SwingIntrospector;
import cx.ath.choisnet.swing.introspection.SwingIntrospectorException;
import cx.ath.choisnet.swing.introspection.SwingIntrospectorRootItem;
import junit.framework.TestCase;

/**
 * @author CC
 *
 */
public class SwingIntrospectorTest extends TestCase
{
    /** Some logs */
    private static Logger slogger = Logger.getLogger(SwingIntrospectorTest.class);
    private SwingIntrospector<JMiiEditorMainJFrame,Mii,MiiGetterIntrospectionItem> debugSwingIntrospector;
    private JMiiEditorMainJFrame frame;

    public void setUp()
        throws FileNotFoundException,
               IOException
    {
        slogger.info( "-- SwingIntrospector --" );
        this.debugSwingIntrospector = new SwingIntrospector<JMiiEditorMainJFrame,Mii,MiiGetterIntrospectionItem>( 
                    new MiiSwingIntrospectorObjectInterface()
                    );

        slogger.info( "-- JMiiEditorVSFrame --" );
        this.frame = new JMiiEditorMainJFrame();
        
        slogger.info( "--" );
    }

    public void test1SwingIntrospectorStructure()
    {
        slogger.info( "-- testSwingIntrospectorStructure() --------------" );
        Map<String, SwingIntrospectorRootItem<JMiiEditorMainJFrame>> itemMap = this.debugSwingIntrospector.getItemMap();
        
        for( Entry<String, SwingIntrospectorRootItem<JMiiEditorMainJFrame>> entry : itemMap.entrySet()) {
            String                      beanName  = entry.getKey();
            SwingIntrospectorRootItem<JMiiEditorMainJFrame>   rootItem  = entry.getValue();

            assertNotNull( "Found a NULL beanName !", beanName );
            
            slogger.info( " - " + beanName + " -> SwingIntrospectorRootItem = " + rootItem );

            assertNotNull( "SwingIntrospectorRootItem is null for " + beanName, rootItem );

            SwingIntrospectorRootItem<JMiiEditorMainJFrame> rootItem2 = this.debugSwingIntrospector.getRootItem( beanName );
            
            // Mismatch ?
            assertTrue( "Mismatch SwingIntrospectorRootItem for " + beanName, rootItem==rootItem2 );
        }

        slogger.info( "--" );
    }

    public void test2PopulateFrame() 
        throws  IntrospectionInvokeException,
                SwingIntrospectorException
    {
        slogger.info( "-- Trying populate() --" );
        final Mii maxMii = MiiTestHelper.getMaxMiiAccordingToConstraits();

        this.debugSwingIntrospector.populateFrameWithException( frame, maxMii );
        slogger.info( "--" );
    }
    
    public void test3PopulateFramePopulateMii() 
        throws  SwingIntrospectorException, 
                IntrospectionException
    {
        slogger.info( "-- Trying populate() --" );
        final Mii maxMii = MiiTestHelper.getMaxMiiAccordingToConstraits();

        this.debugSwingIntrospector.populateFrameWithoutException( frame, maxMii );

        slogger.info(  "-- Set a new Mii from frame." );
        Mii miiCpy = new Mii();

        this.debugSwingIntrospector.populateObjectWithException( frame, miiCpy );

        slogger.info(  "-- Compare MII." );
        String cmpStr = maxMii.advanceCompareTo( miiCpy );

        slogger.info(  "-- cmpStr = " + cmpStr );
        assertTrue( "Diff. cmpMinStr :" + cmpStr, cmpStr == null );
        slogger.info( "--" );
    }
}
